package com.luck.easyfoodbatch.ui;

import android.os.Bundle;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.text.format.Formatter;
import android.widget.TextView;

import com.luck.easyfoodbatch.R;
import com.luck.easyfoodbatch.custom.ChartView;
import com.luck.easyfoodbatch.custom.Utility;
import com.luck.easyfoodbatch.entity.AppInfo;
import com.luck.easyfoodbatch.util.DataCleanManager;
import com.luck.easyfoodbatch.util.FileUtils;

import java.io.File;
import java.lang.reflect.InvocationTargetException;

public class ClearCacheActivity extends AppCompatActivity {
    private ChartView chartView;
    private TextView tv_clear_show;
    private String otherSize;   //其他应用所占空间
    private String userableSize;  //用户可用空间
    private String totalClearSize = "0 KB";  //共清理多少内存
    private long tempSize = 0;
    private AppInfo appinfo=new AppInfo();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_clear_cache);

        try {
            Utility.getPkgSize(ClearCacheActivity.this, "com.luck.easyfoodbatch",appinfo);
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }

        initView();
        initData();
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                chartView.setSizeText(userableSize ,otherSize ,Formatter.formatFileSize(ClearCacheActivity.this,appinfo.getAppSize() ));
                chartView.show();
            }
        },100);

    }

    private void initData() {
        try {
            String path = getApplicationContext().getCacheDir().getPath();
            File file=new File(path);
            tempSize=Utility.getFolderSize(file);
            FileUtils.deleteRichTextImage();
            DataCleanManager.cleanExternalCache(this);
            totalClearSize = Formatter.formatFileSize(ClearCacheActivity.this, tempSize);

        } catch (Exception e) {
            e.printStackTrace();
        }
        userableSize = Formatter.formatFileSize(ClearCacheActivity.this, Utility.getTotalInternalMemorySize() - Utility
                .getAvailableInternalMemorySize()).replace(" ", "");
        otherSize = Formatter.formatFileSize(ClearCacheActivity.this, Utility.getAvailableInternalMemorySize()).replace(" ", "");
        float totalMemory = Utility.getTotalInternalMemorySize() / (1000 * 1000);
        float remainMemory = Utility.getAvailableInternalMemorySize() / (1000 * 1000);
        chartView.setScale(totalMemory, appinfo.getAppSize() / (1000 * 1000), remainMemory);
    }

    private void initView() {
        chartView = (ChartView) findViewById(R.id.chartView);
        tv_clear_show = (TextView) findViewById(R.id.tv_clear_show);
        chartView.setChartViewEndHandler(new ChartView.IChartViewEndHandler() {
            @Override
            public void handlerOnAnimationEnd() {
                tv_clear_show.setText(String.format("已清理临时文件%s", totalClearSize));
            }
        });
    }
}
